home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / PROSIO.C < prev    next >
C/C++ Source or Header  |  1992-02-03  |  8KB  |  255 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/users/cph/src/microcode/RCS/prosio.c,v 1.7 1992/02/04 04:36:56 cph Exp $
  4.  
  5. Copyright (c) 1987-92 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* Primitives to perform I/O to and from files. */
  36.  
  37. #include "scheme.h"
  38. #include "prims.h"
  39. #include "osio.h"
  40.  
  41. #ifndef CLOSE_CHANNEL_HOOK
  42. #define CLOSE_CHANNEL_HOOK(channel)
  43. #endif
  44.  
  45. static Tchannel
  46. DEFUN (arg_to_channel, (argument, arg_number),
  47.        SCHEME_OBJECT argument AND
  48.        int arg_number)
  49. {
  50.   if (! ((INTEGER_P (argument)) && (integer_to_long_p (argument))))
  51.     error_wrong_type_arg (arg_number);
  52.   {
  53.     fast long channel = (integer_to_long (argument));
  54.     if (! ((channel >= 0) || (channel < OS_channel_table_size)))
  55.       error_wrong_type_arg (arg_number);
  56.     return (channel);
  57.   }
  58. }
  59.  
  60. Tchannel
  61. DEFUN (arg_channel, (arg_number), int arg_number)
  62. {
  63.   fast Tchannel channel =
  64.     (arg_to_channel ((ARG_REF (arg_number)), arg_number));
  65.   if (! (OS_channel_open_p (channel)))
  66.     error_bad_range_arg (arg_number);
  67.   return (channel);
  68. }
  69.  
  70. DEFINE_PRIMITIVE ("CHANNEL-CLOSE", Prim_channel_close, 1, 1,
  71.   "Close file CHANNEL-NUMBER.")
  72. {
  73.   PRIMITIVE_HEADER (1);
  74.   {
  75.     fast Tchannel channel = (arg_to_channel ((ARG_REF (1)), 1));
  76.     if (OS_channel_open_p (channel))
  77.       {
  78.     CLOSE_CHANNEL_HOOK (channel);
  79.     OS_channel_close (channel);
  80.       }
  81.   }
  82.   PRIMITIVE_RETURN (UNSPECIFIC);
  83. }
  84.  
  85. DEFINE_PRIMITIVE ("CHANNEL-TABLE", Prim_channel_table, 0, 0,
  86.   "Return a vector of all channels in the channel table.")
  87. {
  88.   PRIMITIVE_HEADER (0);
  89.   {
  90.     Tchannel channel;
  91.     for (channel = 0; (channel < OS_channel_table_size); channel += 1)
  92.       if (OS_channel_open_p (channel))
  93.     obstack_grow ((&scratch_obstack), (&channel), (sizeof (Tchannel)));
  94.   }
  95.   {
  96.     unsigned int n_channels =
  97.       ((obstack_object_size ((&scratch_obstack))) / (sizeof (Tchannel)));
  98.     if (n_channels == 0)
  99.       PRIMITIVE_RETURN (SHARP_F);
  100.     {
  101.       Tchannel * channels = (obstack_finish (&scratch_obstack));
  102.       Tchannel * scan_channels = channels;
  103.       SCHEME_OBJECT vector =
  104.     (allocate_marked_vector (TC_VECTOR, n_channels, 1));
  105.       SCHEME_OBJECT * scan_vector = (VECTOR_LOC (vector, 0));
  106.       SCHEME_OBJECT * end_vector = (scan_vector + n_channels);
  107.       while (scan_vector < end_vector)
  108.     (*scan_vector++) = (long_to_integer (*scan_channels++));
  109.       obstack_free ((&scratch_obstack), channels);
  110.       PRIMITIVE_RETURN (vector);
  111.     }
  112.   }
  113. }
  114.  
  115. DEFINE_PRIMITIVE ("CHANNEL-TYPE", Prim_channel_type, 1, 1,
  116.   "Return (as a nonnegative integer) the type of CHANNEL.")
  117. {
  118.   PRIMITIVE_HEADER (1);
  119.   PRIMITIVE_RETURN
  120.     (long_to_integer ((long) (OS_channel_type (arg_channel (1)))));
  121. }
  122.  
  123. DEFINE_PRIMITIVE ("CHANNEL-READ", Prim_channel_read, 4, 4,
  124.   "Read characters from CHANNEL, storing them in STRING.\n\
  125. Third and fourth args START and END specify the substring to use.\n\
  126. Attempt to fill that substring unless end-of-file is reached.\n\
  127. Return the number of characters actually read from CHANNEL.")
  128. {
  129.   PRIMITIVE_HEADER (4);
  130.   CHECK_ARG (2, STRING_P);
  131.   {
  132.     SCHEME_OBJECT buffer = (ARG_REF (2));
  133.     long length = (STRING_LENGTH (buffer));
  134.     long end = (arg_index_integer (4, (length + 1)));
  135.     long start = (arg_index_integer (3, (end + 1)));
  136.     long nread =
  137.       (OS_channel_read ((arg_channel (1)),
  138.             (STRING_LOC (buffer, start)),
  139.             (end - start)));
  140.     PRIMITIVE_RETURN ((nread < 0) ? SHARP_F : (long_to_integer (nread)));
  141.   }
  142. }
  143.  
  144. DEFINE_PRIMITIVE ("CHANNEL-WRITE", Prim_channel_write, 4, 4,
  145.   "Write characters to CHANNEL, reading them from STRING.\n\
  146. Third and fourth args START and END specify the substring to use.")
  147. {
  148.   PRIMITIVE_HEADER (4);
  149.   CHECK_ARG (2, STRING_P);
  150.   {
  151.     SCHEME_OBJECT buffer = (ARG_REF (2));
  152.     long length = (STRING_LENGTH (buffer));
  153.     long end = (arg_index_integer (4, (length + 1)));
  154.     long start = (arg_index_integer (3, (end + 1)));
  155.     long nwritten =
  156.       (OS_channel_write ((arg_channel (1)),
  157.              (STRING_LOC (buffer, start)),
  158.              (end - start)));
  159.     PRIMITIVE_RETURN ((nwritten < 0) ? SHARP_F : (long_to_integer (nwritten)));
  160.   }
  161. }
  162.  
  163. DEFINE_PRIMITIVE ("CHANNEL-BLOCKING?", Prim_channel_blocking_p, 1, 1,
  164.   "Return #F iff CHANNEL is in non-blocking mode.\n\
  165. Otherwise, CHANNEL is in blocking mode.\n\
  166. If CHANNEL can be put in non-blocking mode, #T is returned.\n\
  167. If it cannot, 0 is returned.")
  168. {
  169.   PRIMITIVE_HEADER (1);
  170.   {
  171.     int result = (OS_channel_nonblocking_p (arg_channel (1)));
  172.     PRIMITIVE_RETURN
  173.       ((result < 0)
  174.        ? (LONG_TO_UNSIGNED_FIXNUM (0))
  175.        : (BOOLEAN_TO_OBJECT (result == 0)));
  176.   }
  177. }
  178.  
  179. DEFINE_PRIMITIVE ("CHANNEL-NONBLOCKING", Prim_channel_nonblocking, 1, 1,
  180.   "Put CHANNEL in non-blocking mode.")
  181. {
  182.   PRIMITIVE_HEADER (1);
  183.   OS_channel_nonblocking (arg_channel (1));
  184.   PRIMITIVE_RETURN (UNSPECIFIC);
  185. }
  186.  
  187. DEFINE_PRIMITIVE ("CHANNEL-BLOCKING", Prim_channel_blocking, 1, 1,
  188.   "Put CHANNEL in blocking mode.")
  189. {
  190.   PRIMITIVE_HEADER (1);
  191.   OS_channel_blocking (arg_channel (1));
  192.   PRIMITIVE_RETURN (UNSPECIFIC);
  193. }
  194.  
  195. DEFINE_PRIMITIVE ("MAKE-PIPE", Prim_make_pipe, 0, 0,
  196.   "Return a cons of two channels, the reader and writer of a pipe.")
  197. {
  198.   PRIMITIVE_HEADER (0);
  199.   {
  200.     SCHEME_OBJECT result = (cons (SHARP_F, SHARP_F));
  201.     Tchannel reader;
  202.     Tchannel writer;
  203.     OS_make_pipe ((&reader), (&writer));
  204.     SET_PAIR_CAR (result, (long_to_integer (reader)));
  205.     SET_PAIR_CDR (result, (long_to_integer (writer)));
  206.     PRIMITIVE_RETURN (result);
  207.   }
  208. }
  209.  
  210. DEFINE_PRIMITIVE ("CHANNEL-REGISTERED?", Prim_channel_registered_p, 1, 1,
  211.   "Return #F iff CHANNEL is registered for selection.")
  212. {
  213.   PRIMITIVE_HEADER (1);
  214.   PRIMITIVE_RETURN
  215.     (BOOLEAN_TO_OBJECT (OS_channel_registered_p (arg_channel (1))));
  216. }
  217.  
  218. DEFINE_PRIMITIVE ("CHANNEL-REGISTER", Prim_channel_register, 1, 1,
  219.   "Register CHANNEL for selection.")
  220. {
  221.   PRIMITIVE_HEADER (1);
  222.   OS_channel_register (arg_channel (1));
  223.   PRIMITIVE_RETURN (UNSPECIFIC);
  224. }
  225.  
  226. DEFINE_PRIMITIVE ("CHANNEL-UNREGISTER", Prim_channel_unregister, 1, 1,
  227.   "Unregister CHANNEL for selection.")
  228. {
  229.   PRIMITIVE_HEADER (1);
  230.   OS_channel_unregister (arg_channel (1));
  231.   PRIMITIVE_RETURN (UNSPECIFIC);
  232. }
  233.  
  234. DEFINE_PRIMITIVE ("CHANNEL-SELECT-THEN-READ", Prim_channel_select_then_read, 4, 4,
  235.   "Like CHANNEL-READ, but also watches registered input channels.\n\
  236. If there is no input on CHANNEL, returns #F.\n\
  237. If there is input on some other registered channel, returns -2.\n\
  238. If the status of some subprocess changes, returns -3.\n\
  239. If an interrupt occurs during the read, returns -4.")
  240. {
  241.   PRIMITIVE_HEADER (4);
  242.   CHECK_ARG (2, STRING_P);
  243.   {
  244.     SCHEME_OBJECT buffer = (ARG_REF (2));
  245.     long length = (STRING_LENGTH (buffer));
  246.     long end = (arg_index_integer (4, (length + 1)));
  247.     long start = (arg_index_integer (3, (end + 1)));
  248.     long nread =
  249.       (OS_channel_select_then_read ((arg_channel (1)),
  250.                     (STRING_LOC (buffer, start)),
  251.                     (end - start)));
  252.     PRIMITIVE_RETURN ((nread == (-1)) ? SHARP_F : (long_to_integer (nread)));
  253.   }
  254. }
  255.